package cn.xq.pethome.shop.service.impl;

import cn.xq.pethome.constants.BaseConstants;
import cn.xq.pethome.employee.domain.Employee;
import cn.xq.pethome.employee.mapper.EmployeeMapper;
import cn.xq.pethome.shop.domain.Shop;
import cn.xq.pethome.shop.domain.ShopAuditLog;
import cn.xq.pethome.shop.mapper.ShopAuditLogMapper;
import cn.xq.pethome.shop.mapper.ShopMapper;
import cn.xq.pethome.shop.query.ShopQuery;
import cn.xq.pethome.shop.service.IShopService;
import cn.xq.pethome.base.service.impl.BaseServiceImpl;
import cn.xq.pethome.utils.AjaxUtil;
import cn.xq.pethome.utils.BAiDuAI_AuthService;
import cn.xq.pethome.utils.BusinessException;
import cn.xq.pethome.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xieqi
 * @since 2023-01-08
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Value("${baiduAi_url}")
    private String baiduAi_url;
    @Value("${baiduAi_ak}")
    private String baiduAi_ak;
    @Value("${baiduAi_sk}")
    private String baiduAi_sk;

    @Override
    public void insert(Shop shop) {
        if(shop.getName()==null){
            throw new BusinessException("商家姓名不能为空!");
        }
        if(shop.getAddress()==null){
            throw new BusinessException("地址不能为空!");
        }
        if(shop.getTel()==null){
            throw new BusinessException("电话不能为空!");
        }

        //检查铭感词汇,访问数据库之前进行验证
        try {
            String param = "text=" +shop.getName();
            String accessToken = BAiDuAI_AuthService.getAuth(baiduAi_ak,baiduAi_sk);
            String result = HttpUtil.post(baiduAi_url, accessToken, param);
            JSONObject jsonObject= JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if(!"合规".equals(conclusion)){
                throw new BusinessException("店铺名不合规，请修改！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long fingquerycount = shopMapper.fingquerycount(shopQuery);
        if(fingquerycount!=0){
            throw new BusinessException("["+shop.getName()+"]店铺名重复！");
        }

        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//审核状态1（待审核）
       //从前端shop数据中获取Employee数据（shop实现类已包含employee（admin））
        Employee admin = shop.getAdmin();

        //在管理员表单上添加admin数据
        employeeMapper.insert(admin);
        //管理员表单中获取id，然后设置给shop的adminid
        shop.setAdminId(admin.getId());
        //将修改好的shop数据进行添加
        shopMapper.insert(shop);
        //将shop表单的id传给employee（admin）的shop_id
        admin.setShop_id(shop.getId());
        //对employee进行修改（前面已经添加）
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //日志记录，补全参数
        shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopAuditLog.setAuditId(333l);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.insert(shopAuditLog);
        //店铺状态修改
        Long shopId = shopAuditLog.getShopId();
        Shop fingone = shopMapper.fingone(shopId);
        fingone.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopMapper.update(fingone);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //日志记录，补全参数
        shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopAuditLog.setAuditId(333l);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.insert(shopAuditLog);
        //店铺状态修改
        Long shopId = shopAuditLog.getShopId();
        Shop fingone = shopMapper.fingone(shopId);
        fingone.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopMapper.update(fingone);
    }
}
